import dayjs from 'dayjs'
import rt from 'dayjs/plugin/relativeTime'
// 语言包
// import 'dayjs/locale/zh-cn'
// import store from '@/store'

export const dateFilter = (val, format = 'YYYY-MM-DD') => {
	if (!isNaN(val)) {
		val = parseInt(val)
	}

	return dayjs(val).format(format)
}

// 加载相对时间插件
dayjs.extend(rt)
function relativeTime(val) {
	if (!isNaN(val)) {
		val = parseInt(val)
	}
	return dayjs()
		.locale(store.getters.language === 'zh' ? 'zh-cn' : 'en')
		.to(dayjs(val))
}

export const formatDate = (numb) => {
	const time = new Date((numb - 1) * 24 * 3600000 + 1)
	time.setYear(time.getFullYear() - 70)
	const year = time.getFullYear() + ''
	const month = time.getMonth() + 1 + ''
	const date = time.getDate() - 1 + ''
	return (
		year +
		'-' +
		(month < 10 ? '0' + month : month) +
		'-' +
		(date < 10 ? '0' + date : date)
	)
}
export const dateFormat = (dateData) => {
	var date = new Date(dateData)
	var y = date.getFullYear()
	var m = date.getMonth() + 1
	m = m < 10 ? '0' + m : m
	var d = date.getDate()
	d = d < 10 ? '0' + d : d
	const time = y + '-' + m + '-' + d
	return time
}

export default (app) => {
	app.config.globalProperties.$filters = {
		dateFilter,
		relativeTime,
		dateFormat,
	}
}
